/*
 * Copyright (c) 2010-2020 IISI.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of IISI.
 */
package com.iisigroup.log.analysis.rdbms.dao;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 */
public class CauseDao extends TableJDBCDao{
    //================================================
    //== [Enumeration types] Block Start
    //== [Enumeration types] Block End 
    //================================================
    //== [static variables] Block Start
    private final static Logger LOGGER = LoggerFactory.getLogger(CauseDao.class);
    //== [static variables] Block Stop 
    //================================================
    //== [instance variables] Block Start
    //== [instance variables] Block Stop 
    //================================================
    //== [static Constructor] Block Start
    //== [static Constructor] Block Stop 
    //================================================
    //== [Constructors] Block Start (含init method)
    //== [Constructors] Block Stop 
    //================================================
    //== [Static Method] Block Start
    //== [Static Method] Block Stop 
    //================================================
    //== [Accessor] Block Start
    //== [Accessor] Block Stop 
    //================================================
    //== [Overrided Method] Block Start (Ex. toString/equals+hashCode)
    public void inserCauseEventData(final int uid ,  final String  desc){
        final Connection conn = TableJDBCDao.getInstance();         
        final String sqlTemplate = "INSERT INTO CAUSE (UID, KEY_CAUSE ) VALUES (%s ,'%s'  ) ;";
        Statement stmt; 
        try {
            stmt = conn.createStatement(); 
            String sql = String.format(sqlTemplate, uid,desc); 
            stmt.executeUpdate(sql);
            stmt.close();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;            
        } finally {
            closeConnection(conn);
        }
    } 
    public Map<Integer, String> getMapData() {
        final Map<Integer, String> result = new HashMap<Integer, String>();
        final Connection conn = TableJDBCDao.getInstance();
        Statement stmt;
        try {
            stmt = conn.createStatement();
            final String sqloffline = "select  *  from CAUSE ;  ";
            final ResultSet rs = stmt.executeQuery(sqloffline);
            while (rs.next()) {
                // Retrieve by column name
                final String cause = rs.getString("key_cause"); 
                int uid = rs.getInt("UID");
                result.put(uid, cause);
            }
            rs.close();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
        } finally {
            closeConnection(conn);
        }
        return result;
    }
    public Map<String, Integer> getreverseMapData() {
        final Map<String, Integer> result = new HashMap<String, Integer>();
        final Connection conn = TableJDBCDao.getInstance();
        Statement stmt;
        try {
            stmt = conn.createStatement();
            final String sqloffline = "select  *  from CAUSE ;  ";
            final ResultSet rs = stmt.executeQuery(sqloffline);
            while (rs.next()) {
                // Retrieve by column name
                final String cause = rs.getString("key_cause"); 
                int uid = rs.getInt("UID");
                result.put(cause, uid );
            }
            rs.close();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
        } finally {
            closeConnection(conn);
        }
        return result;
    }
    public Integer getUid(final String keyCause) {
        final Connection conn = TableJDBCDao.getInstance();
        Integer result =null;
        Statement stmt;
        try {
            stmt = conn.createStatement();
            final String sql = String.format("select  uid  from CAUSE where key_cause ='%s';  " , keyCause.trim());
            final ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {
                // Retrieve by column name
                int uid = rs.getInt("UID");
                return uid;
            }
            rs.close();
        } catch (SQLException e) {
             LOGGER.error(e.getMessage() , e) ;
        } finally {
            closeConnection(conn);
        }
        return result;
    }
    //== [Overrided Method] Block Stop 
    //================================================
    //== [Method] Block Start
    //####################################################################
    //## [Method] sub-block : 
    //####################################################################    
    //== [Method] Block Stop 
    //================================================
    //== [Inner Class] Block Start
    //== [Inner Class] Block Stop 
    //================================================
}
